package com.tjl.warehouse.controller;

import com.tjl.warehouse.common.ErrorCode;
import com.tjl.warehouse.common.R;
import com.tjl.warehouse.common.ResultUtils;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.model.UserInfo;
import com.tjl.warehouse.model.dto.LoginUser;
import com.tjl.warehouse.model.dto.UserDto;
import com.tjl.warehouse.service.UserInfoService;
import com.tjl.warehouse.utils.JWTUtils;
import com.tjl.warehouse.utils.RedisService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Tang
 * @Date 2024/5/11
 */
@RestController
@Slf4j
@Tag(name = "登录接口")
public class LoginController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    RedisService redisService;


    @GetMapping("/curr-user")
    public R<UserDto> currentUser(HttpServletRequest request){
        String token = request.getHeader("Token");
        int userId = JWTUtils.getUserIdFromToken(token);
        UserInfo byId = userInfoService.getById(userId);
        ThrowUtils.throwIf(byId == null, ErrorCode.FORBIDDEN_ERROR);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(byId, userDto);
        return ResultUtils.success(userDto);

    }

    @PostMapping("/login")
    public R<String> login(@RequestBody LoginUser loginDto) {
        //校验参数
        ThrowUtils.throwIf(loginDto == null,ErrorCode.PARAMS_ERROR);
        if (StringUtils.isAllBlank(loginDto.getVerificationCode(),
                loginDto.getUserPwd(), loginDto.getUserCode())){
            return R.error(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }

        //校验验证码
        checkCode(loginDto);

        UserInfo user = userInfoService.login(loginDto.getUserCode(),loginDto.getUserPwd());
        String token = JWTUtils.generateToken(user.getUserId());

        return ResultUtils.success(token);
    }

    @DeleteMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        String token = request.getHeader("Token");
        int userId = JWTUtils.getUserIdFromToken(token);
        redisService.del("token:" + userId);
        return ResultUtils.success("退出成功");
    }

    //校验验证码 Hutool版本
    public boolean checkCodeByHutool(LoginUser loginDto){
        //获取缓存中的验证码
        String code = redisService.get("captcha:" + loginDto.getCodeKey());
        ThrowUtils.throwIf(code == null, ErrorCode.OPERATION_ERROR,"验证码已过期");
        ThrowUtils.throwIf(!code.equals(loginDto.getVerificationCode()),
                ErrorCode.OPERATION_ERROR,"验证码错误");
        return true;
    }


    //校验验证码
    public boolean checkCode(LoginUser loginDto){
        //获取缓存中的验证码
        ThrowUtils.throwIf(!redisService.hasKey(loginDto.getVerificationCode()),
                ErrorCode.OPERATION_ERROR,"验证码错误");

        return true;
    }

}
